package com.hhs.storage.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("app/login")
public class AppLoginController {
    @Resource
    private UserService userService;
    @Resource
    private JxcMenuService jxcMenuService;
    @Resource
    private YymUserExtService yymUserExtService;
    @Resource
    private YymUserTcLinkService yymUserTcLinkService;
    @Resource
    private YymHuiyuanTcService yymHuiyuanTcService;
    @Resource
    private JxcRoleUserService jxcRoleUserService;

    //登录后跳转到主页
    @RequestMapping("login")
    public Map<String, Object> login(String account, String password) {
        EntityWrapper<User> ew = new EntityWrapper<User>();
        ew.eq("account_", account);
        ew.eq("password_", password);
        List<User> list = userService.selectList(ew);
        if (list.size() != 1) {
            return Result.failJsonResp("用户名或密码错误！");
        } else {
            List<JxcMenu> menulist = jxcMenuService.findByUserId(list.get(0).getId());
            AppUser appuser = new AppUser();
            appuser.setId(list.get(0).getId());
            appuser.setAccount(list.get(0).getAccount());
            appuser.setName(list.get(0).getName());
            EntityWrapper<YymUserExt> ew2 = new EntityWrapper<>();
            ew2.eq("user_id_", list.get(0).getId());
            List<YymUserExt> userextlist = yymUserExtService.selectList(ew2);
            if (userextlist.size() == 1) {
                appuser.setPhotoPath(userextlist.get(0).getPhotoPath());
                appuser.setPhoneNo(userextlist.get(0).getPhoneNo());
                appuser.setQq(userextlist.get(0).getQq());
                appuser.setWxNo(userextlist.get(0).getWxNo());
                appuser.setYe(userextlist.get(0).getYe());
                appuser.setZy(userextlist.get(0).getZy());
            } else {
                appuser.setPhotoPath("");
            }
            appuser.setMenulist(menulist);
            //找用户和套餐
            List<YymUserTcLink> yytc = yymUserTcLinkService.selectList(new EntityWrapper<YymUserTcLink>().eq("user_id_", list.get(0).getId()));
            if (yytc.size() == 1) {
                YymHuiyuanTc tc = yymHuiyuanTcService.selectById(yytc.get(0).getTcId());
                appuser.setHuiyuanFlag(tc.getFlag());
            } else {
                appuser.setHuiyuanFlag("非会员");
            }
            return Result.sucJsonResp(appuser);
        }
    }

    //登录从本地存贮来的
    @RequestMapping("login2")
    public Map<String, Object> login2(String account) {
        EntityWrapper<User> ew = new EntityWrapper<User>();
        ew.eq("account_", account);
        List<User> list = userService.selectList(ew);
        if (list.size() != 1) {
            return Result.failJsonResp("用户名或密码错误！");
        } else {
            List<JxcMenu> menulist = jxcMenuService.findByUserId(list.get(0).getId());
            AppUser appuser = new AppUser();
            appuser.setId(list.get(0).getId());
            appuser.setAccount(list.get(0).getAccount());
            appuser.setName(list.get(0).getName());
            EntityWrapper<YymUserExt> ew2 = new EntityWrapper<>();
            ew2.eq("user_id_", list.get(0).getId());
            List<YymUserExt> userextlist = yymUserExtService.selectList(ew2);
            if (userextlist.size() == 1) {
                appuser.setPhotoPath(userextlist.get(0).getPhotoPath());
                appuser.setPhoneNo(userextlist.get(0).getPhoneNo());
                appuser.setQq(userextlist.get(0).getQq());
                appuser.setWxNo(userextlist.get(0).getWxNo());
                appuser.setYe(userextlist.get(0).getYe());
                appuser.setZy(userextlist.get(0).getZy());
            } else {
                appuser.setPhotoPath("");
            }
            appuser.setMenulist(menulist);
            //找用户和套餐
            List<YymUserTcLink> yytc = yymUserTcLinkService.selectList(new EntityWrapper<YymUserTcLink>().eq("user_id_", list.get(0).getId()));
            if (yytc.size() == 1) {
                YymHuiyuanTc tc = yymHuiyuanTcService.selectById(yytc.get(0).getTcId());
                appuser.setHuiyuanFlag(tc.getFlag());
            } else {
                appuser.setHuiyuanFlag("非会员");
            }
            return Result.sucJsonResp(appuser);
        }
    }

    @RequestMapping(value = "register")
    public Map<String, Object> saveOrUpdate(User user,String tjm) {
        log.info("新增或插入用户信息");

        if (StringUtils.isEmpty(user.getAccount())) {
            return Result.failJsonResp("账号不能为空");
        }
        List<User> ulist = userService.selectList(new EntityWrapper<User>().eq("account_", user.getAccount()));
        if (ulist.size() > 0) {
            return Result.failJsonResp("账号已经有人注册了");
        }
        if(StringUtils.isNotEmpty(tjm)) {
            List<User> tjlist = userService.selectList(new EntityWrapper<User>().eq("account_", tjm));
            if (tjlist.size() > 0) {
                user.setPid(tjlist.get(0).getId());
            }
        }
        user.setCreateTime(new Date());
        userService.saveOrUpdate(user, "");
        JxcRoleUser jru = new JxcRoleUser();
        jru.setUserId(user.getId());
        //给一个一般会员
        jru.setRoleId("11");
        jru.setCreateTime(new Date());
        jxcRoleUserService.insertOrUpdate(jru);
        return userService.saveOrUpdate(user, "");
    }

    @RequestMapping("logout")
    public String logout(String account, String password) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login";
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public JxcMenuService getJxcMenuService() {
        return jxcMenuService;
    }

    public void setJxcMenuService(JxcMenuService jxcMenuService) {
        this.jxcMenuService = jxcMenuService;
    }

    public YymUserExtService getYymUserExtService() {
        return yymUserExtService;
    }

    public void setYymUserExtService(YymUserExtService yymUserExtService) {
        this.yymUserExtService = yymUserExtService;
    }
}
